package com.soundShape.mediators.newCustomer
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.model.ApplicationModel;
	import com.soundShape.views.login.Login;
	import com.soundShape.views.newCustomer.NewCustomer;
	import com.soundShape.vo.CustomerVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.effects.Rotate;
	import mx.managers.PopUpManager;
	
	import nz.co.codec.flexorm.criteria.Criteria;
	
	import spark.components.BorderContainer;
	
	
	
	
	public class NewCustomerMediator extends AbstractViewMediator
	{
		
			
		
				
				public function NewCustomerMediator()
				{
					super(NewCustomer);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():NewCustomer
				{
					return this._view as NewCustomer;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					this.view.btnMenu.addEventListener(MouseEvent.CLICK, handleBtnMenu);
					
					this.view.btnNewCustomer.addEventListener(MouseEvent.CLICK, handleBtnNewCustomer);
					
					this.view.txtInCustomerName.addEventListener(MouseEvent.CLICK, handleTxtInCustomerNameClick);
				}
				
				private function setViews():void
				{
					
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnMenu(mouseEvent:MouseEvent):void
				{
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
				}
				
				
				private function handleBtnNewCustomer(mouseEvent:MouseEvent):void
				{
					 var customerVO:CustomerVO = new CustomerVO();
					 customerVO.address = this.view.txtAraCustomerAddress.text;
					 customerVO.email = this.view.txtInCustomerEmail.text;
					 customerVO.name = this.view.txtInCustomerName.text;
					 customerVO.phone = this.view.txtInCustomerPhone.text;
					 customerVO.reference = this.view.radGrpReferal.selectedValue as String;
					 customerVO.employeeID = this.applicationModel.employeeVO.id;
					 
					 if(!isDuplicateCustomer(customerVO))
					 {
						 this.applicationModel.entityManager.save(customerVO);
						 this.view.lblNewCustomerInfo.text = "Customer addition successful";
						 this.view.lblNewCustomerInfo.setStyle("color", 0x009900);
						 
						 this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.GET_CUSTOMERS));
					 }
					 else
					 {
						 this.view.lblNewCustomerInfo.text = "Attempted duplication of record"
						 this.view.lblNewCustomerInfo.setStyle("color", 0xff0000);
					 }
					
					 resetCustomerForm()
				}
				
				private function isDuplicateCustomer(customerVO:CustomerVO):Boolean
				{
					var isDupe:Boolean = false;
					
					var criteria:Criteria = applicationModel.entityManager.createCriteria(CustomerVO);
					
					var customers:ArrayCollection = applicationModel.entityManager.fetchCriteria(criteria);
					
					for each (var customer:CustomerVO in customers)
					{
						if((customer.name == customerVO.name)&&(customer.address == customerVO.address)&&(customer.email == customerVO.email))
						{
							isDupe = true;
							break;
						}
					}
					
				return isDupe;
				}
				
				private function resetCustomerForm():void
				{
					this.view.txtAraCustomerAddress.text = "";
					this.view.txtInCustomerEmail.text = "";
					this.view.txtInCustomerName.text = "";
					this.view.txtInCustomerPhone.text = "";
					this.view.radGrpReferal.selection = null;
				}
				
				private function handleTxtInCustomerNameClick(mouseEvent:MouseEvent):void
				{
					this.view.lblNewCustomerInfo.text = "";
				}
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				
			
		}
		
	}
